//
//  HSReplayView.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/10/11.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit

class HSReplayView: UIView {
    //点赞人姓名
    @IBOutlet weak var likeNameL: UILabel!
    //点赞分割线
    @IBOutlet weak var likeLineV: UIView!
    //创建一个保存回复数据标签的数组
    var replayLArray : [UILabel] = []
    
    //视图模型
    var messageVM : HSMessageViewModel?{
        didSet{
            
            setupUI()
        }
    }
    fileprivate func setupUI(){
        //添加之前移除回复列表
        for l in replayLArray {
            l.removeFromSuperview()
        }
        //清空数组
        replayLArray.removeAll()
        
        //获得点赞人数
        let likeN = messageVM?.message.likenum ?? 0
       
            likeNameL.attributedText = messageVM?.likeNameAttribute
        
        //获得回复消息数量
        let replayNum = messageVM?.message.replaylist?.count ?? 0
        
        //处理分割线显示
        if likeN > 0 && replayNum > 0 {
            likeLineV.isHidden = false
        }else{
            likeLineV.isHidden = true
        }
        //当有回复内容的时候才执行后面的代码
        guard let arr = messageVM?.replayAttributeArray else {
            return
        }
        //循环添加标签视图
        for attStr in arr {
            //初始化
            let l = UILabel(attributeTitle: attStr, fontSize: 13)
            addSubview(l)
            //同时记录当前添加的标签
            replayLArray.append(l)
        }
        
        //添加标签视图的约束   i 下标  l是值
        for (i,l) in replayLArray.enumerated() {
            if i == 0 {
                if likeN == 0 {
                    l.snp.makeConstraints({ (make) in
                        make.left.equalTo(8)
                        make.top.equalTo(12)
                        make.right.equalTo(-8)
                    })  }else{
                    l.snp.makeConstraints({ (make) in
                        make.left.equalTo(8)
                        make.top.equalTo(likeLineV.snp.bottom).offset(5)
                        make.right.equalTo(-8)
                    })
                }
            }else{
                l.snp.makeConstraints({ (make) in
                    make.left.equalTo(8)
                    make.top.equalTo(replayLArray[i-1].snp.bottom).offset(5)
                    make.right.equalTo(-8)
                })
            }
        }
        
    }
    //从 xib 引用过来的话，要重写这个方法
    override func awakeFromNib() {
        
    }
}
